package com.cms.work.service.impl.sys;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.work.core.service.impl.BaseServiceImpl;
import com.cms.work.dao.sys.ISysRoleResourceDao;
import com.cms.work.entity.sys.SysRoleResource;
import com.cms.work.service.sys.ISysRoleResourceService;

/**
 * @describe 角色资源关联模块
 * @author YY
 * @version v1.0
 * @date 2016-11-23 14:03:05
 **/
@Service
public class SysRoleResourceServiceImpl extends BaseServiceImpl<SysRoleResource, Integer> implements ISysRoleResourceService {
	
    @Autowired
    private ISysRoleResourceDao sysRoleResourceDao;
	
	@Override
	public void save(SysRoleResource sysRoleResource) {
	    if (sysRoleResource.getId() == null || sysRoleResource.getId() == 0) {
            sysRoleResourceDao.insertSelective(sysRoleResource);
        } else {
            sysRoleResourceDao.updateByPrimaryKeySelective(sysRoleResource);
        }
	}

	@Override
	public void addRoleAndResourceLink(Integer roleId, List<Integer> resources) {
        List<SysRoleResource> roleResourceList = new ArrayList<SysRoleResource>();
        if(resources != null && resources.size() > 0){
        	sysRoleResourceDao.deleteByRoleId(roleId);
            for (int i = 0; i < resources.size(); i++) {
            	SysRoleResource roleResource = new SysRoleResource(roleId, resources.get(i));
            	roleResourceList.add(roleResource);
            }
            sysRoleResourceDao.batchInsertRoleResource(roleResourceList);
        }
        
	}
}

